Skip to content

test: Add tests for CTX manager usage on PEP0249.#448

Closed
pvital wants to merge 1 commit intomasterfrom
pep0249_ctx_mngr_ut
Closed

test: Add tests for CTX manager usage on PEP0249.#448
pvital wants to merge 1 commit intomasterfrom
pep0249_ctx_mngr_ut

Conversation

@pvital
Copy link
Member

@pvital pvital commented Aug 30, 2023

Add the test_connect_ctx_mngr UT to test the span creation when using the psycopg2 connect with context manager, and the test_connect_cursor_ctx_mngr UT when using the psycopg2 connect and cursos context manager.

@pvital pvital requested review from Ferenc- and GSVarsha August 30, 2023 14:07
Copy link
Member

@Ferenc- Ferenc- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a TC where the db is not managed, but the cursor is.
Please cover the rest of the instrumentations where PEP0248 is used, like mysql.

@GSVarsha
Copy link
Contributor

GSVarsha commented Aug 31, 2023

Hi @pvital ,
I've also been adding tests to db modules that conform to PEP-249 in this branch.
Do you think we should somehow merge changes from these 2 branches?
I've also covered pymysql and mysqlclient here.

Add the test_connect_ctx_mngr UT to test the span creation when
using the psycopg2 connect with context manager, and the
test_connect_cursor_ctx_mngr UT when using the psycopg2 connect
and cursos context manager.

Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
@pvital pvital force-pushed the pep0249_ctx_mngr_ut branch from f67eabc to ccf7ce0 Compare August 31, 2023 06:56
@pvital pvital requested a review from Ferenc- August 31, 2023 07:12
@pvital
Copy link
Member Author

pvital commented Aug 31, 2023

I am closing this PR once we use #450 to track this fix.

@pvital pvital closed this Aug 31, 2023
self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

self.assertEqual(test_span.t, db_span.t)
self.assertEqual(db_span.p, test_span.s)

self.assertEqual(None, db_span.ec)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be assertIsNone

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

cursor.fetchone()
self.db.close()

assert(result >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

cursor.fetchone()
self.db.close()

assert(result >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

result = cursor.execute("""SELECT * from users""")
cursor.fetchone()

assert(result >= 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_ctx_mngr(self):
result = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context manager does not create a new scope, the result = cursor.execute( statement is enough. Please remove this line, luckily this is not Pascal or ANSI C, where you have to declare the variables way ahead of assigning them.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_cursor_ctx_mngr(self):
result = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_cursor_ctx_mngr(self):
result = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_ctx_mngr(self):
result = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_cursor_ctx_mngr(self):
result = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port'])

def test_connect_cursor_ctx_mngr(self):
result = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove.

Copy link
Member

@Ferenc- Ferenc- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a preliminary review. With some minor formalities commented for now.

@pvital
Copy link
Member Author

pvital commented Aug 31, 2023

@Ferenc- as mentioned here, this PR is closed. Thanks anyway.

@pvital pvital deleted the pep0249_ctx_mngr_ut branch November 12, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants